package zhaolu.com.example.Delicacy.Utils;

import zhaolu.com.example.Delicacy.Activity.FragmentActivity;
import zhaolu.com.example.Delicacy.Activity.MainActivity;
import zhaolu.com.example.Delicacy.Entity.Customer;
import zhaolu.com.example.Delicacy.Entity.Order;
import zhaolu.com.example.Delicacy.MySQL.OrderDAO;
import zhaolu.com.example.Delicacy.R;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.sql.BatchUpdateException;
import java.util.List;

import zhaolu.com.example.Delicacy.Entity.ItemBean;

public class MyAdapter extends BaseAdapter {
    private List<ItemBean> beanList;
    private LayoutInflater layoutInflater;
    private Context context;
    private String username;

    public MyAdapter(Context context, List<ItemBean> beanList, String username) {
        this.context = context;
        this.beanList = beanList;
        this.username = username;
        layoutInflater = LayoutInflater.from(this.context);
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return beanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        // 创建视图控件
        ViewHolder viewHolder = new ViewHolder();
        if (view == null) {
            view = layoutInflater.inflate(R.layout.order_menu, viewGroup, false);
            ImageView imageView = view.findViewById(R.id.orderMenu_image);
            TextView textView1 = view.findViewById(R.id.orderMenu_title);
            TextView textView2 = view.findViewById(R.id.orderMenu_description);
            Button add = view.findViewById(R.id.orderMenu_add);
            viewHolder.imageView = imageView;
            viewHolder.textView1 = textView1;
            viewHolder.textView2 = textView2;
            viewHolder.add = add;
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        // 填充数据
        ItemBean itemBean = beanList.get(i);
        viewHolder.imageView.setImageResource(itemBean.getImage());
        viewHolder.textView1.setText(itemBean.getTitle());
        viewHolder.textView2.setText(itemBean.getDescription());
        viewHolder.add.setText("添加" + i);
        ((ViewGroup) view).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        viewHolder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtils.showShortMsg(context, "已将" + i + "菜品添加至订单！");
                Order order = new Order(i, username);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final int iRow = OrderDAO.addOrder(order);
                    }
                }).start();
            }
        });

        return view;
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView1;
        TextView textView2;
        Button add;
    }

}
